<?php
/**
 *多页导出
 */
namespace App\Http\Controllers\Excel;

use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithTitle;
use App\Models\Result\TraineeResult;
use App\Models\Activities\Event;
use App\Models\System\Dicitem;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Font;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithHeadings;



class TraineeResultSheet implements FromArray, WithTitle,WithHeadings,WithStyles
{
    protected $array;
    private $type;
    private $trainee_id;
    private $term_id;
    private $request;

    public function __construct(int $type, string $trainee_id , string $term_id, array $request)
    {
        $this->type = $type;
        $this->trainee_id = $trainee_id;
        $this->term_id = $term_id;
        $this->request = $request;
    }

    /**
     * @return Builder
     */
    public function array(): array
    {      
        $trainee_id = $this->trainee_id;
        $term_id = $this->term_id;
        $type = $this->type;
        $user_id = $this->request['user_id'];
  
        $dic = [];
         $dicitem = Dicitem::get(['diccode','itemcode','itemname'])->toArray();
         foreach($dicitem as $k=>$v){
            $dic[$v['diccode']][$v['itemcode']] = $v;
         }

         $data = [];
         if($this->type == '8'){
            $result = Event::orWhere([['term_id','=',$term_id],['user_id','like','%'.$user_id.'%']])
            ->orWhere([['term_id','=',$term_id],['host','like','%'.$user_id.'%']])
            ->orWhere([['term_id','=',$term_id],['personnel','like','%'.$user_id.'%']])
            ->get()->toArray();
         }else{
            $result = TraineeResult::where([['trainee_id','=',$trainee_id],['term_id','=',$term_id],['type','=',$type]])->with(['trainee:id,name'])->get()->toArray();
         }       

        foreach($result as $k=>$v){
            if($this->type == '1'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['term_name'] = $v['term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['end_date'] = $v['end_date'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['duty_text'] = $v['duty_text'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '2'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['term_name'] = $v['term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['end_date'] = $v['end_date'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['duty_text'] = $v['duty_text'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '3'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['term_name'] = $v['term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['place'] = $v['place'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['areas'] = $v['areas'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '4'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['term_name'] = $v['term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['place'] = $v['place'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['areas'] = $v['areas'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '5'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['term_name'] = $v['term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['place'] = $v['place'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['result_type'] = ($dic && !empty($v['result_type']) && !empty($dic['OUTCOME_CATEGORY'][$v['result_type']]))?$dic['OUTCOME_CATEGORY'][$v['result_type']]['itemname']:'';
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '6'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['term_name'] = $v['term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['place'] = $v['place'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['areas'] = $v['areas'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '7'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['term_name'] = $v['term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['place'] = $v['place'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['course_type_text'] = $v['course_type_text'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '8'){
                $data[$k]['studio_name'] = $v['studio_name'];
                $data[$k]['name'] = $this->request['name'];
                $data[$k]['current_unit'] = $this->request['current_unit'];
                $data[$k]['term_name'] = $this->request['term_name'];
                $data[$k]['theme'] = $v['theme'];
                $data[$k]['start_time'] = $v['start_time'];
                $data[$k]['end_time'] = $v['end_time'];
                $data[$k]['place'] = $v['place'];
                $data[$k]['style'] = $v['style_text'];
                $data[$k]['section'] = $v['section'];
                $data[$k]['subject'] = $v['subject'];
            }else if($this->type == '9'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['title1_text'] =  $v['title1_text'] = ($dic && !empty($v['title1']) && !empty($dic['RESULT_TITLE'][$v['title1']]))?$dic['RESULT_TITLE'][$v['title1']]['itemname']:'';
                $data[$k]['title2_text'] = $v['title2_text'] = ($dic && !empty($v['title2']) && !empty($dic['RESULT_TITLE'][$v['title2']]))?$dic['RESULT_TITLE'][$v['title2']]['itemname']:'';
                $data[$k]['start_date'] = $v['start_date'];
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '10'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['else_term_name'] = $v['else_term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];         
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '11'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['else_term_name'] = $v['else_term_name'];
                $data[$k]['item_name'] = $v['item_name'];
                $data[$k]['level'] = ($dic && !empty($v['level']) && !empty($dic['DECLARE_LEVEL'][$v['level']]))?$dic['DECLARE_LEVEL'][$v['level']]['itemname']:'';;
                $data[$k]['start_date'] = $v['start_date'];         
                $data[$k]['unit'] = $v['unit'];
                $data[$k]['remark'] = $v['remark'];
            }else if($this->type == '12'){
                $data[$k]['trainee_name'] = $v['trainee']['name'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['current_unit'] = $v['current_unit'];
                $data[$k]['subject_text'] = ($dic && !empty($v['subject']) && !empty($dic['TEACHING_SUBJECT'][$v['subject']]))?$dic['TEACHING_SUBJECT'][$v['subject']]['itemname']:'';
                $data[$k]['degree_text'] = ($dic && !empty($v['degree']) && !empty($dic['RESULT_EDUCATION'][$v['degree']]))?$dic['RESULT_EDUCATION'][$v['degree']]['itemname']:'';
                $data[$k]['remark'] = $v['remark'];
            }
           
        }
       
        return $data;   
      
    }

    public function headings(): array
    {  
        switch($this->type){
            case '1':  return ['工作室名称','姓名','所在单位','学期','课题（项目）名称','级别','立项时间','结项时间','组织单位','责任','进展及成效说明']; break;
            case '2': return ['工作室名称','姓名','所在单位','学期','学科建设名称','级别','立项时间','结项时间','组织单位','责任','进展及成效说明']; break;
            case '3': return ['工作室名称','姓名','所在单位','学期','名称','级别','立项时间','地点','组织单位','责任人群','进展及成效说明']; break;
            case '4': return ['工作室名称','姓名','所在单位','学期','名称','级别','立项时间','地点','组织单位','辐射人群','进展及成效说明']; break;
            case '5': return ['工作室名称','姓名','所在单位','学期','名称','级别','立项时间','地点','组织单位','成果类别','进展及成效说明']; break;
            case '6': return ['工作室名称','姓名','所在单位','学期','名称','级别','立项时间','地点','组织单位','参与人群','进展及成效说明']; break;
            case '7': return ['工作室名称','姓名','所在单位','学期','名称','级别','立项时间','地点','组织单位','课程形态','进展及成效说明']; break;
            case '8': return ['工作室名称','姓名','所在单位','学期','活动主题','开始时间','结束时间','活动地点','活动形式','所属学段','所属学科']; break;
            case '9': return ['工作室名称','姓名','所在单位','启动时职称级别','届满时职称级别','职级晋升时间','组织单位','进展及成效说明']; break;
            case '10': return ['工作室名称','姓名','所在单位','学期','名称','级别','时间','组织单位','备注说明']; break;
            case '11': return ['工作室名称','姓名','所在单位','学期','名称','级别','时间','组织单位','备注说明']; break;
            case '12': return ['工作室名称','姓名','所在单位','学科','学历','备注说明']; break;
            default :  return ''; break;
        }

    }

    /**
     * @return string
     */
    public function title(): string
    {
        switch($this->type){
            case '1': return '攻坚课题'; break;
            case '2': return '学科建设'; break;
            case '3': return '教学展示'; break;
            case '4': return '交流辐射'; break;
            case '5': return '学术成果'; break;
            case '6': return '建设实践校'; break;
            case '7': return '课程开发'; break;
            case '8': return '研修活动'; break;
            case '9': return '职称晋升'; break;
            case '10': return '资质荣誉'; break;
            case '11': return '获奖情况'; break;
            case '12': return '学历提升'; break;
            default :  return ''; break;
        }

    }

    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text.
            1    => ['font' => ['bold' => true]],

        ];
    }

    
   





    
    
}

?>
